/*

*Copyright (c) 2016, -, zking. All rights reserved.

*/
package com.zkingsoft.actions.visitor;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.model.Nav;
import com.zkingsoft.model.ShowBox;
import com.zkingsoft.model.ShowElement;
import com.zkingsoft.model.ShowElementCate;
import com.zkingsoft.model.ShowPoint;
import com.zkingsoft.services.FunctionService;
import com.zkingsoft.services.MessageService;
import com.zkingsoft.services.NavService;
import com.zkingsoft.services.RolePwoerService;
import com.zkingsoft.services.ShowBoxService;
import com.zkingsoft.services.ShowElementCateService;
import com.zkingsoft.services.ShowElementService;
import com.zkingsoft.services.ShowPointService;
import com.zkingsoft.services.UsersService;

/**
 * @description 用于封装一些在前台界面显示时常用到的方法，
 * @author Matrix-J
 * @email 935090232@qq.com
 * @date 2015-10-08
 * @version 1.0
 */
@Controller
public class VistorBase extends BaseController {
	/**
	 * 当前公司的id
	 */
	public static Long companyId = 11L;
	@Resource
	public UsersService usersService;
	@Resource
	public FunctionService functionService;
	@Resource
	public RolePwoerService rolePwoerService;
	@Resource
	public NavService navService;
	@Resource
	public ShowPointService showPointService;
	@Resource
	public ShowBoxService showBoxService;
	@Resource
	public ShowElementService showElementService;
	@Resource
	public ShowElementCateService showElementCateService;
	@Resource
	public MessageService messageService;

	/**
	 * 下面这些对象 用于支持model查询，这里只是声明这些变量，在使用时候需要自己new出来
	 */
	public ShowElement showElementQuery;
	public ShowElementCate showElementCateQuery;
	public ShowBox showboxQuery;
	public ShowPoint showPointQuery;
	public Nav navQuery;

	/**
	 * 在request中存放一个showPoint ，没有找则是一个空的showpint;
	 * 
	 * @param key
	 *            存在request中的名称
	 * @param name
	 *            showPoint的text值
	 * @param companyId
	 *            公司id
	 * @return 
	 */
	public ShowPoint setPointToRequestByName(String key, String name) {
		ShowPoint point = new ShowPoint();
		showPointQuery = new ShowPoint();
		showPointQuery.setText(name);
		showPointQuery.setCompanyId(companyId);
		List<ShowPoint> pList = showPointService.findByModel(showPointQuery);
		if (pList.size() == 1) {
			point = pList.get(0);
		}
		getRequest().setAttribute(key, point);
		return point;
	}

	/**
	 * 在request中存放一个showPoint List
	 * 
	 * @param key
	 *            存在request中的名称
	 * @param name
	 *            showBox 的名称
	 * @param companyId
	 *            公司id
	 * @return 
	 */
	public List<ShowPoint> setPointsByBoxToRequest(String key, String name) {
		List<ShowPoint> list = showPointService.findByBoxName(name, companyId);
		getRequest().setAttribute(key, list);
		return list;
	}
	
	
	/**
	 * 在request中存放一个showelement 并返回
	 * 
	 * @param key
	 *            存在request中的名称
	 * @param name
	 *            showBox 的名称
	 * @param companyId
	 *            公司id
	 */
	public ShowElement setShowElementToRequestByName(String key, String name) {
		ShowElement temp=new ShowElement();
		showElementQuery=new ShowElement();
		showElementQuery.setCompanyId(companyId);
		showElementQuery.setName(name);
		List<ShowElement> list= showElementService.findByModel(showElementQuery);
		if(list.size()==1){
			temp=list.get(0);
		}
		getRequest().setAttribute(key, temp);
		return temp;
	}

}